home *** CD-ROM | disk | FTP | other *** search
/ Clickx 63 / Clickx 63.iso / software / multimedia / mirov204 / Miro_Installer.exe / xulrunner / chrome / toolkit.jar / content / global / customizeCharset.js < prev    next >
Encoding:
JavaScript  |  2006-11-06  |  7.9 KB  |  306 lines

  1. //get prefInt services
  2.  
  3. var availCharsetDict     = [];
  4. var prefBranch           = null; //Preferences Interface
  5. var pref_string_title    = "";
  6. var pref_string_content  = "";
  7. var pref_string_object   = null;
  8.  
  9. function Init()
  10. {
  11.   var applicationArea = "";
  12.  
  13.   if ("arguments" in window && window.arguments[0])
  14.     applicationArea = window.arguments[0];
  15.  
  16.   prefBranch = Components.classes["@mozilla.org/preferences-service;1"];
  17.  
  18.   if (prefBranch) {
  19.     prefBranch = prefBranch.getService(Components.interfaces.nsIPrefBranch);
  20.  
  21.     if (applicationArea.indexOf("mail") != -1) {
  22.       pref_string_title = "intl.charsetmenu.mailedit";
  23.     } else {
  24.     //default is the browser
  25.       pref_string_title = "intl.charsetmenu.browser.static";
  26.     }
  27.  
  28.     pref_string_object = prefBranch.getComplexValue(pref_string_title, Components.interfaces.nsIPrefLocalizedString)
  29.     pref_string_content = pref_string_object.data;
  30.  
  31.     AddRemoveLatin1('add');
  32.   }
  33.  
  34.   LoadAvailableCharSets();
  35.   LoadActiveCharSets();
  36. }
  37.  
  38.  
  39. function readRDFString(aDS,aRes,aProp) 
  40. {
  41.   var n = aDS.GetTarget(aRes, aProp, true);
  42.   if (n)
  43.     return n.QueryInterface(Components.interfaces.nsIRDFLiteral).Value;
  44.   else 
  45.     return "";
  46. }
  47.  
  48.  
  49. function LoadAvailableCharSets()
  50. {
  51.   try {
  52.     var available_charsets_listbox = document.getElementById('available_charsets');
  53.     var rdf=Components.classes["@mozilla.org/rdf/rdf-service;1"].getService(Components.interfaces.nsIRDFService); 
  54.     var kNC_Root = rdf.GetResource("NC:DecodersRoot");
  55.     var kNC_name = rdf.GetResource("http://home.netscape.com/NC-rdf#Name");
  56.     var rdfDataSource = rdf.GetDataSource("rdf:charset-menu"); 
  57.     var rdfContainer = Components.classes["@mozilla.org/rdf/container;1"].getService(Components.interfaces.nsIRDFContainer);
  58.  
  59.     rdfContainer.Init(rdfDataSource, kNC_Root);
  60.     var availableCharsets = rdfContainer.GetElements();
  61.     var charset;
  62.  
  63.     for (var i = 0; i < rdfContainer.GetCount(); i++) {
  64.       charset = availableCharsets.getNext().QueryInterface(Components.interfaces.nsIRDFResource);
  65.       availCharsetDict[i] = new Array(2);
  66.       availCharsetDict[i][0] = readRDFString(rdfDataSource, charset, kNC_name);
  67.       availCharsetDict[i][1] = charset.Value;
  68.  
  69.       AddListItem(document,
  70.                   available_charsets_listbox,
  71.                   availCharsetDict[i][1],
  72.                   availCharsetDict[i][0]);
  73.     }
  74.   }
  75.   catch (e) {}
  76. }
  77.  
  78.  
  79. function GetCharSetTitle(id)
  80. {
  81.   if (availCharsetDict) {
  82.     for (var j = 0; j < availCharsetDict.length; j++) {
  83.       if (availCharsetDict[j][1] == id) {
  84.         return availCharsetDict[j][0];
  85.       }
  86.     }
  87.   }
  88.   return '';
  89. }
  90.  
  91. function AddRemoveLatin1(action)
  92. {
  93.   var arrayOfPrefs = [];
  94.   arrayOfPrefs = pref_string_content.split(', ');
  95.  
  96.   if (arrayOfPrefs.length > 0) {
  97.     for (var i = 0; i < arrayOfPrefs.length; i++) {
  98.       if (arrayOfPrefs[i] == 'ISO-8859-1') {
  99.         if (action == 'remove') {
  100.           arrayOfPrefs[i] = arrayOfPrefs[arrayOfPrefs.length-1];
  101.           arrayOfPrefs.length = arrayOfPrefs.length - 1;
  102.         }
  103.  
  104.         pref_string_content = arrayOfPrefs.join(', ');
  105.         return;
  106.       }
  107.     }
  108.  
  109.     if (action == 'add') {
  110.       arrayOfPrefs[arrayOfPrefs.length] = 'ISO-8859-1';
  111.       pref_string_content = arrayOfPrefs.join(', ');
  112.     }
  113.   }
  114. }
  115.  
  116.  
  117. function LoadActiveCharSets()
  118. {
  119.   var active_charsets = document.getElementById('active_charsets');
  120.   var arrayOfPrefs = [];
  121.   var str;
  122.   var tit;
  123.  
  124.   arrayOfPrefs = pref_string_content.split(', ');
  125.  
  126.   if (arrayOfPrefs.length > 0) {
  127.     for (var i = 0; i < arrayOfPrefs.length; i++) {
  128.       str = arrayOfPrefs[i];
  129.       tit = GetCharSetTitle(str);
  130.       if (str && tit)
  131.         AddListItem(document, active_charsets, str, tit);
  132.     }
  133.   }
  134. }
  135.  
  136. function enable_save()
  137. {
  138.   var save_button = document.documentElement.getButton("accept");
  139.   save_button.removeAttribute('disabled');
  140. }
  141.  
  142.  
  143. function update_buttons()
  144. {
  145.   var available_charsets = document.getElementById('available_charsets');
  146.   var active_charsets = document.getElementById('active_charsets');
  147.   var remove_button = document.getElementById('remove_button');
  148.   var add_button = document.getElementById('add_button');
  149.   var up_button = document.getElementById('up_button');
  150.   var down_button = document.getElementById('down_button');
  151.  
  152.   var activeCharsetSelected = (active_charsets.selectedItems.length > 0);
  153.   remove_button.disabled = !activeCharsetSelected;
  154.  
  155.   if (activeCharsetSelected) {
  156.     up_button.disabled = !(active_charsets.selectedItems[0].previousSibling);
  157.     down_button.disabled = !(active_charsets.selectedItems[0].nextSibling);
  158.   }
  159.   else {
  160.     up_button.disabled = true;
  161.     down_button.disabled = true;
  162.   }
  163.  
  164.   add_button.disabled = (available_charsets.selectedItems.length == 0);
  165. }
  166.  
  167.  
  168.  
  169. function AddAvailableCharset()
  170. {
  171.   var active_charsets = document.getElementById('active_charsets');
  172.   var available_charsets = document.getElementById('available_charsets');
  173.  
  174.   for (var nodeIndex=0; nodeIndex < available_charsets.selectedItems.length;  nodeIndex++)
  175.   {
  176.     var selItem =  available_charsets.selectedItems[nodeIndex];
  177.     
  178.     var charsetname  = selItem.label;
  179.     var charsetid = selItem.id;
  180.     var already_active = false;
  181.  
  182.     for (var item = active_charsets.firstChild; item != null; item = item.nextSibling) {
  183.       if (charsetid == item.id) {
  184.         already_active = true;
  185.         break;
  186.       }//if
  187.  
  188.     }//for
  189.  
  190.     if (already_active == false) {
  191.       AddListItem(document, active_charsets, charsetid, charsetname);
  192.     }//if
  193.  
  194.   }//for
  195.  
  196.   available_charsets.clearSelection();
  197.   enable_save();
  198.  
  199. } //AddAvailableCharset
  200.  
  201.  
  202.  
  203. function RemoveActiveCharset()
  204. {
  205.   var listbox = document.getElementById('active_charsets');
  206.   var nextNode = null;
  207.   var numSelected = listbox.selectedItems.length;
  208.   var deleted_all = false;
  209.  
  210.   var numSelectedItems = listbox.selectedItems.length;
  211.   for (count = 0; count < numSelectedItems; count ++) {
  212.     listbox.removeChild(listbox.selectedItems[0]);
  213.   }
  214.  
  215.   listbox.clearSelection();
  216.  
  217.   enable_save();
  218. } //RemoveActiveCharset
  219.  
  220.  
  221.  
  222. function Save()
  223. {
  224.   // Iterate through the 'active charsets  tree to collect the charsets
  225.   // that the user has chosen.
  226.  
  227.   var active_charsets = document.getElementById('active_charsets');
  228.  
  229.   var charsetid    = "";
  230.   var num_charsets = 0;
  231.   var pref_string_content = '';
  232.  
  233.   for (var item = active_charsets.firstChild; item != null; item = item.nextSibling) {
  234.     charsetid = item.id;
  235.  
  236.     if (charsetid.length > 1) {
  237.       num_charsets++;
  238.  
  239.       //separate >1 charsets by commas
  240.       if (num_charsets > 1)
  241.         pref_string_content = pref_string_content + ", " + charsetid;
  242.       else
  243.         pref_string_content = charsetid;
  244.     }
  245.   }
  246.  
  247.   try
  248.   {
  249.     if (prefBranch) {
  250.       pref_string_object.data = pref_string_content;
  251.       prefBranch.setComplexValue(pref_string_title, Components.interfaces.nsIPrefLocalizedString, pref_string_object);
  252.       window.close();
  253.     }
  254.   }
  255.   catch(ex) {
  256.     confirm('exception' + ex);
  257.   }
  258.   return true;
  259. } //Save
  260.  
  261.  
  262. function MoveUp() {
  263.   var listbox = document.getElementById('active_charsets');
  264.   if (listbox.selectedItems.length == 1) {
  265.     var selected = listbox.selectedItems[0];
  266.     var before = selected.previousSibling
  267.     if (before) {
  268.       listbox.insertBefore(selected, before);
  269.       listbox.selectItem(selected);
  270.       listbox.ensureElementIsVisible(selected);
  271.     }
  272.   }
  273.  
  274.   enable_save();
  275. } //MoveUp
  276.  
  277.  
  278.  
  279. function MoveDown() {
  280.   var listbox = document.getElementById('active_charsets');
  281.   if (listbox.selectedItems.length == 1) {
  282.     var selected = listbox.selectedItems[0];
  283.     if (selected.nextSibling) {
  284.       if (selected.nextSibling.nextSibling)
  285.         listbox.insertBefore(selected, selected.nextSibling.nextSibling);
  286.       else
  287.         selected.parentNode.appendChild(selected);
  288.       listbox.selectItem(selected);
  289.     }
  290.   }
  291.  
  292.   enable_save();
  293. } //MoveDown
  294.  
  295. function AddListItem(doc, listbox, ID, UIstring)
  296. {
  297.   // Create a treerow for the new item
  298.   var item = doc.createElement('listitem');
  299.  
  300.   // Copy over the attributes
  301.   item.setAttribute('label', UIstring);
  302.   item.setAttribute('id', ID);
  303.  
  304.   listbox.appendChild(item);
  305. }
  306.